<?php
namespace App\Services\Game;
use App\DTOS\Admin\ActiveCodeSearchDTO;
use App\DTOS\Admin\ActiveRuleSearchDTO;
use App\Models\Game\ActiveCode;
use App\Models\ActiveRule;
use App\Models\ActiveRuleQd;
use App\Models\Game\ActiveCodeMsg;
use App\Models\Game\Server;


class ActiveCodeService
{
    /**
    * @var  \Illuminate\Database\Eloquent\Builder
    */
    private ActiveCode $model;
    private ActiveCodeMsg $modelmsg;

    public function __construct()
    {
        $this->model = new ActiveCode;
        $this->modelmsg = new ActiveCodeMsg;

    }

    public function paginate(ActiveCodeSearchDTO $DTO)
    {
        $perPage = $DTO->getLimit();
        $page = $DTO->getPage();
        $model = $this->model;
        $code= $DTO->getCode();
        $rewardid= $DTO->getRewardid();
        $type = $DTO->getType();


        /**反馈类型**/
        if (!is_null($code)){
            $model = $model->where('code', $code );
        }
        if (!is_null($rewardid)){
            $model = $model->whereIn('rewardid', $rewardid );
        }
        if (!is_null($type)){
            $model = $model->where('type', $type );
        }


        return $model->orderByDesc("id")->paginate($perPage,'*','page',$page);
    }

    public function paginatelog(ActiveCodeSearchDTO $DTO)
    {
        $perPage = $DTO->getLimit();
        $page = $DTO->getPage();
        $modelmsg = $this->modelmsg;
        $code= $DTO->getCode();
        $rewardid= $DTO->getRewardid();
        $type = $DTO->getType();


        /**反馈类型**/
        if (!is_null($code)){
            $modelmsg = $modelmsg->where('code ', $code );
        }
        if (!is_null($rewardid)){
            $modelmsg = $modelmsg->whereIn('rewardid  ', $rewardid );
        }
        if (!is_null($type)){
            $modelmsg = $modelmsg->where('type ', $type );
        }


        return $modelmsg->orderByDesc("id")->paginate($perPage,'*','page',$page);
    }
    /**
    * @param  ActiveRuleSearchDTO $insertDTO
    * @return  int
    */
    public function insert(ActiveRuleSearchDTO $insertDTO){
        $data = $insertDTO->toArray();
        foreach($data as &$value){
            if (is_array($value)){
                $value= json_encode(array_values($value));
            }
        }
        $data['created_at'] = now();
        unset($data['page']); unset($data['limit']);
        return $this->model->insertGetId($data);
    }

    /**
    * @param  int $id
    * @param  ActiveRuleSearchDTO $updateDTO
    */
    public function updateById($id,ActiveRuleSearchDTO $updateDTO)
    {
        $data = $updateDTO->toArray();
        // 处理 serverGroup
        $serverGroup = $data['serverGroup'];

        if($serverGroup){
            // 将 serverGroup 转换为数组
            if (is_string($serverGroup)) {
                $serverGroupArray = explode(',', $serverGroup);
            } else {
                $serverGroupArray = [$serverGroup]; // 单个值
            }

            // 查询对应的 server_id，去重
            $serverIds = Server::query()
                ->whereIn('group_id', $serverGroupArray)
                ->pluck('server_id')
                ->unique() // 去重
                ->toArray();

            // 将 server_ids 转换为以 * 分隔的字符串
            $data['ServerList'] = implode(',', $serverIds);
        }


        foreach($data as &$value){
            if (is_array($value)){
                $value= json_encode(array_values($value));
            }
        }
        $data['updated_at'] = now();
        unset($data['page']); unset($data['limit']);
        $this->model->where('id',$id)->update($data);
    }

    public function deleteById($id)
    {
        $this->model->where('id',$id)->delete();
    }

    public function deleteByIds($ids)
    {
        if (empty($ids)) return;
        $this->model->whereIn('id',$ids)->delete();
    }
    public function getById($id)
    {
        return $this->model->where('id',$id)->first();
    }


}
